'use strict';

/**
 * @ngdoc function
 * @name afasApp.controller:SupplierCtrl
 * @description
 * # SupplierCtrl
 * Controller of the afasApp
 */

angular.module('afasApp')
  .controller('AgeGroupsCtrl', ['$scope', '$state', 'Services', function ($scope, $state, Services) {

    // $scope.agegroups = [];
    $scope.agegroups = [];
    $scope.newItem = {};

    $scope.slider = {
      min: 1,
      max: 50,
      step: 1,
      value: 7,
      rangeValue : [7,13]
    };

    function applyRemoteData( newData ) {
      $scope.agegroups = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.AGEGROUP_URL)
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.add = function () {
      // console.log($scope.newItem);
      $scope.newItem.LowerBound = $scope.slider.rangeValue[0];
      $scope.newItem.UpperBound = $scope.slider.rangeValue[1];
      console.log($scope.newItem);
      Services.add($scope.AGEGROUP_URL, $scope.newItem)
      .then(function(results){
        console.log(results);
        $scope.newItem = {};
        $state.go( 'agegroups.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.delete = function (item) {
      Services.delete($scope.AGEGROUP_URL, item);
    };
  }])
  .controller('AgeGroupCtrl', ['$scope', 'Services', '$state', '$stateParams', function ($scope, Services, $state, $stateParams) {

    $scope.current = {};
    $scope.currentID = $stateParams.id;
    $scope.current = Services.getItem($scope.AGEGROUP_URL, $stateParams.id);

    $scope.update = function () {
      Services.update($scope.AGEGROUP_URL, $scope.current, $scope.currentID);
      $state.go( 'agegroups.show', {id: $scope.currentID} );
    };
  }]);
